package com.yihu.cms.service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.yihu.beans.*;
import com.yihu.beans.searchCondition.DerivativesPageConditions;
import com.yihu.dataService.hessian.mongodb.interfaceService.DerivativesHService;
import com.yihu.dataService.hessian.resource.ResourceMongoDBManager;

public class DerivativesService {

	public int add(Derivatives derJson) {
		int result = 0;
		try {
			DerivativesHService dhs = (DerivativesHService) ResourceMongoDBManager
					.getSingleton().getBeans("derivativesHSC");
			result = dhs.addDerivatives(derJson);
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			result = 1;
			e1.printStackTrace();
		}
		return result;
	}

	public int del(String id) {
		int result = 0;
		try {
			DerivativesHService dhs = (DerivativesHService) ResourceMongoDBManager
					.getSingleton().getBeans("derivativesHSC");
			result = dhs.delDerivatives(id);
		} catch (MalformedURLException e) {
			result = 1;
			System.out.println(e.toString());
		} catch (IOException e1) {
			result = 2;
			e1.printStackTrace();
		}
		return result;
	}

	public int update(Derivatives derJson) {
		int result = 0;
		try {
			DerivativesHService dhs = (DerivativesHService) ResourceMongoDBManager
					.getSingleton().getBeans("derivativesHSC");
			result = dhs.updateDerivatives(derJson);
		} catch (MalformedURLException e) {
			result = 1;
			System.out.println(e.toString());
		} catch (IOException e1) {
			result = 2;
			e1.printStackTrace();
		}
		return result;
	}

	public Derivatives getById(String id) {
		Derivatives result = null;
		try {
			DerivativesHService dhs = (DerivativesHService) ResourceMongoDBManager
					.getSingleton().getBeans("derivativesHSC");
			result = dhs.getDerivativesById(id);
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return result;
	}

	public List<Derivatives> getPaging(int skip, int limit , DerivativesPageConditions pc) {
		List<Derivatives> result = new ArrayList<Derivatives>();
		try {
			DerivativesHService derivatives = (DerivativesHService) ResourceMongoDBManager
					.getSingleton().getBeans("derivativesHSC");
			result = derivatives.getDerivativesPaging(skip, limit , pc);
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return result;
	}
	
	public long getTotal()
	{
		long result = 0 ;
		try {
			DerivativesHService derivatives = (DerivativesHService) ResourceMongoDBManager
					.getSingleton().getBeans("derivativesHSC");
			result = derivatives.getDerivativesTotal();
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return result;
	}

	public static void main(String args[]) {
		DerivativesService ds = new DerivativesService();

//		Derivatives d = new Derivatives();
//		Users user = new Users();
//		user.setId(2);
//		user.setName("xiaoliu");
//
//		ArtworkAuthor aa = new ArtworkAuthor();
//		aa.setId(2);
//		aa.setName("董杰");
//
//		ArtworkCategory ac = new ArtworkCategory();
//		ac.setId(1);
//		ac.setName("油画");
//
//		CommodityStates cs = new CommodityStates();
//		cs.setId(1);
//		cs.setName("已上架");
//
//		d.setId(4);
//		d.setName("深蓝系列--水");
//		d.setUser(user);
//		d.setPrice(29999.99);
//		d.setCreateTime(new Date());
//		d.setFinishTime(new Date());
//		d.setArtworkAuthor(aa);
//		d.setArtworkCategory(ac);
//		d.setCommodityStates(cs);
//		d.setLike(5);
//		d.setIsRecommend(7);
//		d.setSorts(8);
//		d.setPv(100);
//
//		String str = JSON.toJSONString(d);
//		System.out.println("add data result = " + ds.addDerivatives(str));
		
//		String d = ds.getDerivativesById(1);
//		
//		Derivatives d1 = JSON.parseObject(d, Derivatives.class);
//		
//		d1.setPrice(200.00);
//		
//		System.out.println("update result = " + ds.updateDerivatives(JSON.toJSONString(d1)));
//		
//		System.out.println("update data = " + ds.getDerivativesById(1));

		DerivativesPageConditions dpc = new DerivativesPageConditions();
//		dpc.setMaterialName("陶");
		dpc.setCommodityCategoryId("5");
		// paging
		System.out.println(JSON.toJSONString(ds.getPaging(1, 8 , dpc)));
		
		System.out.println(ds.getTotal());

//		 System.out.println(ds.del(4));
		
//		 System.out.println(JSON.toJSONString(ds.getPaging(1, 10 , null)));

		// get
		// System.out.println(JSON.toJSONString(as.getArtworkById(2)));
		System.out.println("end");
	}



}
